#include "userinfoinputdialog.h"
#include "ui_userinfoinputdialog.h"
#include "QRegularExpressionValidator"
#include<QMediaPlayer>
#include "Beans/userinfobeab.h"
#include "QDateTime"
#include "Config/constant.h"
#include "DBOperation/mysql.h"

UserInfoInputDialog::UserInfoInputDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::UserInfoInputDialog)
{
    ui->setupUi(this);
//    this->setWindowFlags(Qt::Tool | Qt::X11BypassWindowManagerHint | Qt::FramelessWindowHint);
    this->setWindowModality(Qt::WindowModal);
    this->setWindowFlags(Qt::Tool |Qt::FramelessWindowHint);
    this->setProperty("Form", true);
    this->setProperty("CanMove", true);
    //设置窗体标题栏隐藏
//    this->setWindowFlags(Qt::FramelessWindowHint | Qt::WindowSystemMenuHint | Qt::WindowMinimizeButtonHint);

    setBg();
//    initialControl();

    ui->lineEdit_Age->setValidator(new QIntValidator(ui->lineEdit_Age));
    ui->lineEdit_height->setValidator(new QIntValidator(ui->lineEdit_height));
    ui->lineEdit_weight->setValidator(new QIntValidator(ui->lineEdit_weight));
    watcher.addPath("D:/Data/UserConfig.ini");
    connect(&watcher, &QFileSystemWatcher::fileChanged, this, &UserInfoInputDialog::fileChanged);

    QDateTime Time = QDateTime::currentDateTime();
    ui->lineEdit_ID->setText(Time.toString("yyyyMMddhhmmss"));

}

UserInfoInputDialog::~UserInfoInputDialog()
{

    delete ui;
}
void UserInfoInputDialog::Initial()
{
    ui->lineEdit_Age->setText("111");
    ui->lineEdit_ID->setText("");
    ui->lineEdit_Name->setText("");
    ui->lineEdit_height->setText("");
    ui->lineEdit_weight->setText("");
    initialControl();
}
void UserInfoInputDialog::initialControl()
{
//    groupName="UserInfo",key值有 id,name,age,sex
    QSettings *setting=new QSettings("D:/Data/UserConfig.ini",QSettings::IniFormat);
    setting->beginGroup("UserInfo");
    QString state=setting->value("state").toString();
    if(state=="yes"){
        ui->lineEdit_Age->setText(setting->value("age").toString());
        ui->lineEdit_ID->setText(setting->value("id").toString());
        ui->lineEdit_Name->setText(setting->value("name").toString());
        ui->lineEdit_height->setText(setting->value("height").toString());
        ui->lineEdit_weight->setText(setting->value("weight").toString());
        if(setting->value("sex").toString()=="男")
        {
            ui->comboBox->setCurrentIndex(0);
        }else
        {
            ui->comboBox->setCurrentIndex(1);
        }
        setting->setValue("state","no");
    }
    setting->endGroup();
    delete setting;


}
/**
 * 功能：设置背景图片
 * @brief SettingPage::setBg
 */
void UserInfoInputDialog::setBg()
{
//    QPalette pal = this->palette();
//    pal.setBrush(QPalette::Window,QBrush(QPixmap(":/images/share/infodialog_bg.png")));
//    setPalette(pal);
}

void UserInfoInputDialog::on_bt_exit_clicked()
{
    UserInfoBeab uib;
    uib.UserID="";
    done(1);
    this->close();
}


void UserInfoInputDialog::on_bt_ok_clicked()
{

    QString id=ui->lineEdit_ID->text();
    QString name=ui->lineEdit_Name->text();
    QString age=ui->lineEdit_Age->text();
    QString sex=ui->comboBox->currentText();
    QString height=ui->lineEdit_height->text();
    QString weight=ui->lineEdit_weight->text();
    uib.UserID=id;
    uib.UserName=name;
    uib.UserAge=age;
    uib.UserSex=sex;
    uib.UserHeight=height;
    uib.UserWeight=weight;
    qDebug()<<"UserSex"<<sex;
    QDateTime Time = QDateTime::currentDateTime();
    uib.MeasureTime=Time.toString("yyyy-MM-dd hh:mm:ss");//格式化时间


    if(id.length()>0)
    {
        if(age.size()>0)
        {
            if(age.toInt()>0 && age.toInt()<100)
            {
                done(1);
                this->close();
            }else
            {
                ui->lineEdit_Age->setPlaceholderText("年龄范围（0-99）");
            }
        }else
        {
            ui->lineEdit_Age->setPlaceholderText("年龄不能为空");
        }


    }else
    {
        ui->lineEdit_ID->setPlaceholderText("编号不能为空");

    }

}


void UserInfoInputDialog::on_lineEdit_ID_editingFinished()
{
    QString file=Constant::DataBasePath;
    MySql *mySql = new MySql(file);
    QList<DataBeans>dbs=mySql->DataQueryBy(ui->lineEdit_ID->text(),"","");
    if(dbs.size()>0 && ui->lineEdit_ID->text().size()>0)
    {
        ui->lineEdit_Name->setText(dbs.at(dbs.size()-1).UserName);
        ui->lineEdit_Age->setText(dbs.at(dbs.size()-1).UserAge);
        if(dbs.at(dbs.size()-1).UserSex=="男")
        {
            ui->comboBox->setCurrentIndex(0);
        }else
        {
            ui->comboBox->setCurrentIndex(1);
        }
        ui->lineEdit_height->setText(dbs.at(dbs.size()-1).UserHeight);
        ui->lineEdit_weight->setText(dbs.at(dbs.size()-1).UserWeight);
    }
    delete mySql;
}

void UserInfoInputDialog::fileChanged()
{
    initialControl();
}


